package hiWorld.amad.it;

import java.io.BufferedWriter;
import java.sql.Timestamp;

import org.xmlpull.v1.XmlSerializer;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.util.Xml;

public class dbHelper extends SQLiteOpenHelper{
	
	private static final String DB_NAME = "gpsPos.db";
	private static final String DB_TABLE = "mainTable";
	private static final int DB_VERSION = 1;
	
	public static final String KEY_ID = "_id";
	public static final int NAME_COLUMN = 1;
	
	public static final String COL_TIME_NAME = "time";
	public static final int COL_TIME_NUMB = 2;
	public static final String COL_LONG_NAME = "long";
	public static final int COL_LONG_NUMB = 3;
	public static final String COL_LAT_NAME = "lat";
	public static final int COL_LAT_NUMB = 4;
	
	private static final String DB_CREATE = "create table " + DB_TABLE +
		" (" + KEY_ID + " integer primary key autoincrement, " +
		COL_TIME_NAME + " datetime, " +
		COL_LONG_NAME + " real, "+
		COL_LAT_NAME + " real"+ ");";
	
	Context myContext;
	
	private SQLiteDatabase _db;
	

	public dbHelper(Context _context)
	{
		super(_context, DB_NAME, null, DB_VERSION);
		myContext = _context;
	}
	
	public void open()
	{
		_db = getWritableDatabase();
	}
	
	public void close()
	{
		_db.close();
	}
	
	// Insert a new position in the db
	public void addPosition(long millis, int longit, int latit)
	{
		ContentValues newValues = new ContentValues();
		newValues.put(COL_TIME_NAME, new Timestamp(millis).toString());
		newValues.put(COL_LONG_NAME, longit);
		newValues.put(COL_LAT_NAME, latit);
		_db.insert(DB_TABLE, null, newValues);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		Log.w("DBAdapter", "onCreate" + " " + DB_CREATE);
		db.execSQL(DB_CREATE);
	}

	public void dump(BufferedWriter out)
	{
		open();
		
		Cursor c = _db.query(DB_TABLE, null, null, null, null, null, null);  

		c.moveToFirst();
		XmlSerializer serializer = Xml.newSerializer();
	
		try
		{
			serializer.setOutput(out);
			
			serializer.startDocument("UTF-8", true);
			
			serializer.startTag("", "gpx");
			serializer.attribute("", "version", "1.0");
			serializer.attribute("", "creator", "ExpertGPS 1.1 - http://www.topografix.com");
			serializer.attribute("xmlns", "xsi", "http://www.w3.org/2001/XMLSchema-instance");
			serializer.attribute("", "xmlns", "http://www.topografix.com/GPX/1/0");
			serializer.attribute("xsi", "schemaLocation", "http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd");			
			serializer.startTag("", "trkType");
			serializer.startTag("", "number");
			serializer.text("1");
			serializer.endTag("", "number");
			do
			{
				String time = c.getString(1);
				String longit = String.valueOf(c.getFloat(2)/(1e6));
				String latit = String.valueOf(c.getFloat(3)/(1e6));
				

				serializer.startTag("", "trkseg");
				
				serializer.startTag("", "wpt");
				serializer.attribute("", "lat",latit);
				serializer.attribute("", "lon",longit);

				serializer.startTag("", "time");
				serializer.text(time);
				serializer.endTag("", "time");

				serializer.endTag("", "wpt");
				serializer.endTag("", "trkseg");
				
			} while (c.moveToNext());
			
			serializer.endTag("", "trkType");
			serializer.endTag("", "gpx");

		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
		
		close();
	}
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + DB_NAME);
		db.execSQL(DB_CREATE);
	}
}